package com.lms.dao.weixin;

import com.lms.dao.BaseDao;
import com.lms.util.WeixinUtil.pojo.WeixinAccess_token;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class WeixinDaoImpl implements WeixinDao{
    private PreparedStatement pstm;
    private ResultSet rs;
    @Override
    public int bind(Connection connection, String userCode, String openID) {
        return 0;
    }

    @Override
    public WeixinAccess_token getAccess_token(Connection connection) throws SQLException {
        WeixinAccess_token weixinAccess_token = new WeixinAccess_token();
        if (connection!=null){
            String sql="select access_token,access_token_createTime from weixin";
            rs = BaseDao.execute(connection, pstm, rs, sql, new Object[]{});
            while (rs.next()){
                weixinAccess_token.setAccess_token(rs.getString("access_token"));
                weixinAccess_token.setAccess_token_createTime(rs.getLong("access_token_createTime"));
            }
            BaseDao.closeResource(null,pstm,rs);
        }
        return weixinAccess_token;
    }

    @Override
    public void setAccess_token(Connection connection, WeixinAccess_token access_token) throws SQLException {
        if (connection!=null){
            String sql="update weixin set access_token=?,access_token_createtime=?";
            BaseDao.execute(connection,pstm,sql,new Object[]{access_token.getAccess_token(),access_token.getAccess_token_createTime()});
            BaseDao.closeResource(null,pstm,rs);
        }
    }

    @Override
    public boolean searchOpenID(Connection connection, String userCode) throws SQLException {
        String openID=null;
        int count=0;
        Connection newConnection=BaseDao.getConnection();
        if (connection!=null){
            String sql=null;
            /*先判断是root还是user*/
            sql="select COUNT(*) as count from user_account where userCode=?";
            rs=BaseDao.execute(connection, pstm,rs, sql, new Object[]{userCode});
            while (rs.next()){
                count=rs.getInt("count");
            }
            if (count>0){
                /*是user*/
                sql="select openID from user_account where userCode=?";
                rs = BaseDao.execute(newConnection, pstm, this.rs, sql, new Object[]{userCode});
                while (rs.next()){
                    openID = rs.getString("openID");
                }
                newConnection.close();
            }else {
                /*是root*/
                sql="select openID from root_account where userCode=?";
                rs = BaseDao.execute(newConnection, pstm, this.rs, sql, new Object[]{userCode});
                while (rs.next()){
                    openID = rs.getString("openID");
                }
                newConnection.close();
            }
            BaseDao.closeResource(null,pstm, this.rs);
        }
        return openID!=null;
    }

    @Override
    public boolean bindOpenID(Connection connection, String userCode,String openID) throws SQLException {
        int flag=0;
        int count=0;
        if (connection!=null){
            String sql=null;
            /*先判断是root还是user*/
            Connection newConnection=BaseDao.getConnection();
            sql="select COUNT(*) as count from user_account where userCode=?";
            rs=BaseDao.execute(newConnection, pstm,rs, sql, new Object[]{userCode});
            while (rs.next()){
                count=rs.getInt("count");
            }
            newConnection.close();
            if (count>0) {
                /*是user*/
                sql = "update user_account set openID=? where userCode=?";
                flag = BaseDao.execute(connection, pstm, sql, new Object[]{openID, userCode});
            }else {
                /*是root*/
                sql = "update root_account set openID=? where userCode=?";
                flag = BaseDao.execute(connection, pstm, sql, new Object[]{openID, userCode});
            }

            BaseDao.closeResource(null,pstm, this.rs);
        }
        return flag>0;
    }

    @Override
    public boolean unbindOpenID(Connection connection, String openID) throws SQLException {
        int flag=0;
        int count=0;
        if (connection!=null){
            String sql=null;
            /*先判断是root还是user*/
            Connection newConnection=BaseDao.getConnection();
            sql="select COUNT(*) as count from user_account where openID=?";
            rs=BaseDao.execute(newConnection, pstm,rs, sql, new Object[]{openID});
            while (rs.next()){
                count=rs.getInt("count");
            }
            newConnection.close();
            if (count>0) {
                /*是user*/
                sql = "update user_account set openID=null where openID=?";
                flag = BaseDao.execute(connection, pstm, sql, new Object[]{openID});
            }else {
                /*是root*/
                sql = "update root_account set openID=null where openID=?";
                flag = BaseDao.execute(connection, pstm, sql, new Object[]{openID});
            }

            BaseDao.closeResource(null,pstm, this.rs);
        }
        return flag>0;
    }
}
